package com.ftqh.tutorial.api.v1;

import com.alibaba.fastjson.JSONObject;
import com.ftqh.tutorial.Entity.Order;
import com.ftqh.tutorial.web.vo.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("api/v1/orders")
public class OrderControllerV1 {

    @Autowired
    private OrderServiceV1 orderServiceV1;

    @GetMapping("{id}")
    public ResponseEntity<JSONObject> findOne(@PathVariable("id") Long id) throws Exception{
        Order order = orderServiceV1.findOne(id);
        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("userId", order.getUserId());
        json.put("amount", order.getAmount());
        return ResponseEntity.ok(json);
    }
    @GetMapping()
    public ResponseEntity<List<Order>> findByUserId(Integer userId) throws Exception{
        return ResponseEntity.ok(orderServiceV1.findByUserId(userId));
    }
}
